
#!bin/sh
root_dir="/private/var/mobile/Containers/Data/Application"
function find_app_data_dir()
{
    for element in `ls $1`
    do
        dir_or_file=$1"/"$element
        if [ -d $dir_or_file ];then
            # echo $dir_or_file
            for item in `ls -a "$dir_or_file" `
            do
                # echo $item
                if [[ $item =~ "plist" ]];
                then

                    test=`cat $dir_or_file"/"$item`
                    if [[ $test =~ $2 ]];
                    then
                         echo $dir_or_file
                    fi
                fi
            done
        else
            echo $dir_or_file
        fi
    done
}

function get_file()
{
    for element in `ls $1`
    do
        dir_or_file=$1"/"$element
        if [ -d $dir_or_file ];
        then
            get_file $dir_or_file
        else
            echo $dir_or_file
        fi
    done
}


if [[ $1 == "find_run_data" ]];
then
    find_app_data_dir $root_dir $2
elif [[ $1 == "copy_to_pc" ]];
then
    des_pass=$2
    # $2 /private/var/mobile/Containers/Data/Application/83256EB9-80B9-4686-9657-5EDE69F00203/Library/Preferences/com.sogou.sogouinput.plist
    # $3 /Users/libo/Desktop/
    expect -c "
    spawn scp -P 2222 root@127.0.0.1:$3 $4
    expect \"password:\"
    send \"${des_pass}\r\"
    expect eof
    "
elif [[ $1 == "copy_to_ios" ]];
then
    des_pass=$2
    # $2 /Users/libo/Desktop/test.sh

    expect -c "
    spawn scp -P 2222 $3 root@127.0.0.1:/var/root/
    expect \"password:\"
    send \"${des_pass}\r\"
    expect eof
    "
elif [[ $1 == "get_file" ]];
then
    get_file $2
fi